﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRRSRoguelike.Helpers;
using System.Drawing;

namespace RRRSRoguelike.Managers
{
    /// <summary>
    /// Stores a list of actions along with the keys mapped to those actions
    /// Checks action map against the inputmanager
    /// </summary>
    public class ActionManager
    {

        #region variables
        public ActionMap[] ActionMap;
        public Dictionary<string, Point> DirectionVectors;
        InputManager InputManager = GameManager.Instance.InputManager;
        #endregion

        #region methods
        private void Initialize(int numberOfActions)
        {
            ActionMap = new ActionMap[numberOfActions];
            for (int i = 0; i < numberOfActions; i++)
                ActionMap[i] = new ActionMap();

            SetupDirectionVectors();
        }

        public void SetButtons(Actions Action, ConsoleKey Key)
        {
            ActionMap[(int)Action].ConsoleKeys.Add(Key);
        }
        
        public void SetButtons(Actions Action, List<ConsoleKey> Keys)
        {
            ActionMap[(int)Action].ConsoleKeys.AddRange(Keys);
        }

        public bool IsActionPushed(Actions Action)
        {
            for (int i = 0; i < ActionMap[(int)Action].ConsoleKeys.Count(); i++)
            {
                if (InputManager.KeyPush(ActionMap[(int)Action].ConsoleKeys[i]))
                {
                    return true;
                }
            }
            return false;
        }

        //public bool IsActionReleased(Actions Action)
        //{
        //    for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++)
        //    {
        //        if (InputManager.KeyReleased(ActionMaps[(int)Action].ConsoleKeys[i]))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        //public bool IsActionDown(Actions Action) 
        //{
        //    for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++) 
        //    {
        //        if (InputManager.KeyDown(ActionMaps[(int)Action].ConsoleKeys[i]))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        private void SetupDirectionVectors()
        {
            DirectionVectors = new Dictionary<string, Point>();
            DirectionVectors.Add(Direction.N.ToString(), new Point(0, -1));
            DirectionVectors.Add(Direction.NE.ToString(), new Point(1, -1));
            DirectionVectors.Add(Direction.E.ToString(), new Point(1, 0));
            DirectionVectors.Add(Direction.SE.ToString(), new Point(1, 1));
            DirectionVectors.Add(Direction.S.ToString(), new Point(0, 1));
            DirectionVectors.Add(Direction.SW.ToString(), new Point(-1, 1));
            DirectionVectors.Add(Direction.W.ToString(), new Point(-1, 0));
            DirectionVectors.Add(Direction.NW.ToString(), new Point(-1, -1));
        }

        public void SetDefaultKeys()
        {
            Initialize(Enum.GetNames(typeof(Actions)).Length);
            SetButtons(Actions.North, ConsoleKey.UpArrow);
            SetButtons(Actions.South,  ConsoleKey.DownArrow);
            SetButtons(Actions.East,  ConsoleKey.RightArrow);
            SetButtons(Actions.West, ConsoleKey.LeftArrow);
            SetButtons(Actions.North, ConsoleKey.NumPad8);
            SetButtons(Actions.South, ConsoleKey.NumPad5);
            SetButtons(Actions.East, ConsoleKey.NumPad6);
            SetButtons(Actions.West, ConsoleKey.NumPad4);
            SetButtons(Actions.JumpNorth, ConsoleKey.W);
            SetButtons(Actions.JumpSouth, ConsoleKey.S);
            SetButtons(Actions.JumpEast, ConsoleKey.D);
            SetButtons(Actions.JumpWest, ConsoleKey.A);
            SetButtons(Actions.ShootNorth, ConsoleKey.U);
            SetButtons(Actions.ShootSouth, ConsoleKey.J);
            SetButtons(Actions.ShootEast, ConsoleKey.K);
            SetButtons(Actions.ShootWest, ConsoleKey.H);
            SetButtons(Actions.Look, ConsoleKey.L);
            SetButtons(Actions.Wait, ConsoleKey.OemPeriod);
            SetButtons(Actions.Quit, ConsoleKey.Q);SetButtons(Actions.Quit, ConsoleKey.Escape);
            SetButtons(Actions.Help, ConsoleKey.F1);
            SetButtons(Actions.FollowToggle, ConsoleKey.F2);
            SetButtons(Actions.SoundToggle, ConsoleKey.F3);
            SetButtons(Actions.ChangeAnimationSpeed, ConsoleKey.F4);
             
        }
        #endregion

    }

}
